PHP Beyond the Web by Rob Aley
Author:Rob Aley
Language: eng
Format: epub, pdf
Publisher: Apress, Berkeley, CA
Further Reading
“Semaphore Functions” section in the PHP manual www.php.net/manual/en/ref.sem.php
“What Is Wrong with PHP’s Semaphore Extension” by Jonathon Hill http://jonathonhill.net/2012-12-08/what-is-wrong-with-phps-semaphore-extension/
Shared Memory
Shared memory is a simple and widely supported method of passing data between two or more processes, and it really is as simple as it sounds. A process can create a segment of memory, with a key as a unique ID, assign standard Unix-type permissions to it, and then it and other processes can read, write, and delete data from that segment as necessary (and as permissions allow).
Shared memory is probably the fastest way of sharing information between two processes. There is no disk I/O or database access, for instance, to slow you down and no intermediate message broker to spend time processing and distributing the information. The downside is that you have to manage the whole process yourself, although this isn’t as hard as it sounds.
Shared memory is supported in two extensions , the System V sysvshm extension (part of the semaphore extension, like sysvsem described in the previous section) and the more recent shmop extension. We will look at the latter, as it is based on the C shm API, which makes it easier to share data with non-PHP programs (unlike the former, which uses its own proprietary data format, making it hard to share with anything other than PHP). The shmop extension is also usually faster than sysvshm as it stores its data in a raw form. As with semaphores, this extension is not available on Windows.
Like files, shared memory can become corrupt if multiple processes try to write to it at the same time. In our next example, we don’t need to worry about locking the shared memory segment, as only one process will be writing to it; we won’t include any locking code for brevity and clarity. Where locking is an issue, you can use the semaphore method outlined in the previous section to ensure that only one process writes to the memory segment at a time. Using multiple memory segments at the same time by the same or different processes doesn’t require locking, as long as only one process is writing to a specific segment at a given time.
It’s time to look at an example of shared memory in action. The following are two scripts. The first script, generator.php , generates an array of three random numbers every second, encodes them as JSON, and puts them into a shared memory segment. The second script, display.php, retrieves that data and outputs it to the terminal. To try these, open two terminal windows and run one in each at the same time.
generator.php
<?php
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12719)
Hello! Python by Anthony Briggs(10020)
The Mikado Method by Ola Ellnestam Daniel Brolund(9893)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9889)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9860)
Dependency Injection in .NET by Mark Seemann(9432)
Hit Refresh by Satya Nadella(8877)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8402)
The Kubernetes Operator Framework Book by Michael Dame(8029)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7848)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7817)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7817)
Grails in Action by Glen Smith Peter Ledbrook(7787)
Practical Computer Architecture with Python and ARM by Alan Clements(7769)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7730)
Robo-Advisor with Python by Aki Ranin(7718)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7668)
Building Low Latency Applications with C++ by Sourav Ghosh(7616)
Svelte with Test-Driven Development by Daniel Irvine(7605)
